<?php

require_once 'domain/Channel.php';
require_once 'datatypes/ChannelInfo.php';
require_once(APPPATH . 'libraries/MY_Model.php');

class M_channel extends MY_Model {

	function M_channel() {
		parent::MY_Model('channels', 'suite_channel_id');
	}

	/*	 * ***************************************************
	 * GENERAL
	 * ************************************************** */

	protected function afromEntityToDb(Channel $channel) {
		return array(
			'suite_channel_id' => $channel->suite_id,
			'channel_id' => $channel->id,
			'channel_name' => $channel->name,
			'channel_description' => $channel->description,
			'channel_active' => $channel->active,
			'channel_broadcasting' => $channel->broadcasting,
			'channel_last_update' => $this->getDateToDb($channel->last_update),
			'user_id' => $channel->user_id
		);
	}

	protected function afromDbToEntity(array $p_channel) {
		$c = new Channel();
		$c->suite_id = $p_channel['suite_channel_id'];
		$c->id = $p_channel['channel_id'];
		$c->name = $p_channel['channel_name'];
		$c->description = $p_channel['channel_description'];
		$c->active = $p_channel['channel_active'];
		$c->broadcasting = $p_channel['channel_broadcasting'];
		$c->last_update = $this->getDateFromDb($p_channel['channel_last_update']);
		$c->user_id = $p_channel['user_id'];
		return $c;
	}

	/*	 * ***************************************************
	 * ACCESO A DATOS
	 * ************************************************** */

	function getChannelByUser($userid) {
		return $this->getByWhat('user_id', $userid);
	}

	function getByChannelId($channel_id) {
		return $this->getByWhat('channel_id', $channel_id);
	}

	function get_search_cantPages($search, $cant, $ceibal=0) {
		/*
		  $this->db->like('channel_name',$search);
		  $this->db->from($this->TABLE_NAME);
		  $ret = $this->db->count_all_results(); //no anda
		  //echo $this->db->last_query();
		  return ceil($ret/$cant);
		  / */
		$sql = "SELECT count(*) as cant FROM " . $this->TABLE_NAME . " WHERE channel_name LIKE ? and channel_active=1 and channel_broadcasting=1 and channel_ceibal=$ceibal";
		$search = "%" . $search . "%";
		$query = $this->db->query($sql, array($search));
		$ret = $query->result_array();
		return ceil(($ret[0]["cant"]) / $cant);
		//*/
	}

	function get_search_list($search, $page, $cant, $ceibal=0) {
		$this->db->like('channel_name', $search);
		$this->db->where(array('channel_active' => 1, 'channel_broadcasting' => 1, 'channel_ceibal' => $ceibal));
		$limit_inf = ($page - 1) * $cant;
		$query = $this->db->get($this->TABLE_NAME, $cant, $limit_inf);
		return $this->fromArrayDbToArrayEntity($query->result_array());
	}

	function get_channels_count($search) {
		$sql = "SELECT count(*) as cant FROM " . $this->TABLE_NAME . " WHERE channel_name LIKE ?";
		$search = "%" . $search . "%";
		$query = $this->db->query($sql, array($search));
		$ret = $query->result_array();
		return $ret[0]["cant"];
	}

	function get_all_channels($search, $page, $cant) {
		$floor = ($page-1) * $cant;
		$sql = "
			SELECT c.suite_channel_id, c.channel_active, c.channel_name, user_login, count(denuncia_id) as cant_denuncias
			FROM ".$this->TABLE_NAME." c LEFT JOIN ".TABLE_PREFIX."denuncias d ON c.suite_channel_id = d.suite_channel_id
                    LEFT JOIN ".TABLE_PREFIX."users u ON u.user_id = c.user_id
			WHERE  channel_name LIKE ?
			GROUP BY suite_channel_id
			LIMIT $floor,$cant";
		$search = "%" . $search . "%";


		$query = $this->db->query($sql, array($search));
		//echo $this->db->last_query();
		$resultset = $query->result_array();
		$ret = array();
		foreach ($resultset as $row) {
			$ci = new ChannelInfo();
			$ci->suite_id = $row["suite_channel_id"];
			$ci->name = $row["channel_name"];
			$ci->user_name = isset($row["user_login"]) ? $row["user_login"]:"Ceibal";
			$ci->cant_denuncias = $row["cant_denuncias"];
			$ci->active = $row["channel_active"];
			$ret[] = $ci;
		}
		return $ret;
	}

	function create(Channel $channel) {
		$channel->last_update = date_create();
		return parent::create($channel);
	}

	function delete_channel($channel_id) {
		$this->db->delete($this->TABLE_NAME, array('suite_channel_id' => $channel_id));
		return 0;
	}

}

?>
